﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace StockMentor
{
    public partial class frmDateFirma : Form
    {
        SqlConnection conn;
        DataSet dsFirma;
        CurrencyManager crManager;
        public frmDateFirma(SqlConnection conn)
        {
            InitializeComponent();
            this.conn = conn;
        }

        private void btnInchide_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnModifica_Click(object sender, EventArgs e)
        {
            EnableDisableControls(true);

        }

        private void EnableDisableControls(bool bEnable)
        {
            txtAdresa.Enabled = bEnable;
            txtAdresaWeb.Enabled = bEnable;
            txtCapSocial.Enabled = bEnable;
            txtCif.Enabled = bEnable;
            txtDenFirma.Enabled = bEnable;
            txtEmail.Enabled = bEnable;
            txtFax.Enabled = bEnable;
            txtJudet.Enabled = bEnable;
            txtNrFact.Enabled = bEnable;
            txtOras.Enabled = bEnable;
            txtRegCom.Enabled = bEnable;
            txtSerieFact.Enabled = bEnable;
            txtNumarNir.Enabled = bEnable;
            txtSerieNir.Enabled = bEnable;
            txtTelefon.Enabled = bEnable;
            rbDa.Enabled = bEnable;
            rbNu.Enabled = bEnable;
            
        }

        private void btnSalveaza_Click(object sender, EventArgs e)
        {
            EnableDisableControls(false);
            CustomProcedures.UPDATEFIRMA(System.DateTime.Now,
                                         this.txtDenFirma.Text,
                                         this.txtCif.Text,
                                         this.txtRegCom.Text,
                                         this.txtSerieNir.Text,
                                         Convert.ToInt32(this.txtNumarNir.Text),
                                         this.txtSerieFact.Text,
                                         Convert.ToInt32(this.txtNrFact.Text),
                                         this.txtTelefon.Text,
                                         this.txtFax.Text,
                                         this.txtEmail.Text,
                                         this.txtAdresaWeb.Text,
                                         this.txtAdresa.Text,
                                         this.txtOras.Text,
                                         this.txtJudet.Text,
                                         Convert.ToDouble(this.txtCapSocial.Text),
                                         this.rbDa.Checked?"1":"0") ;

        }

        private void frmDateFirma_Load(object sender, EventArgs e)
        {
            EnableDisableControls(false);
            CustomProcedures.conn = this.conn;

            dsFirma = new DataSet();
            SqlDataAdapter daFirma = new SqlDataAdapter("SELECT TOP 1 * FROM firma ORDER BY firma_data DESC", conn);
            daFirma.Fill(dsFirma, "firma");

            SetControlDataBinding(dsFirma);

            crManager = (CurrencyManager)BindingContext[dsFirma.Tables["firma"]];
           
        }

        private void SetControlDataBinding(DataSet dsFirma)
        {
            txtDenFirma.DataBindings.Clear();
            txtCif.DataBindings.Clear();
            txtRegCom.DataBindings.Clear();
            txtSerieNir.DataBindings.Clear();
            txtNumarNir.DataBindings.Clear();
            txtSerieFact.DataBindings.Clear();
            txtNrFact.DataBindings.Clear();
            txtTelefon.DataBindings.Clear();
            txtFax.DataBindings.Clear();
            txtEmail.DataBindings.Clear();
            txtAdresa.DataBindings.Clear();
            txtAdresaWeb.DataBindings.Clear();
            txtOras.DataBindings.Clear();
            txtJudet.DataBindings.Clear();
            txtCapSocial.DataBindings.Clear();

            txtDenFirma.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_denumire");
            txtCif.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_cif");
            txtRegCom.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_regcom");
            txtSerieNir.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_recep_serie");
            txtNumarNir.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_recep_nr");
            txtSerieFact.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_fact_serie");
            txtNrFact.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_fact_nr");
            txtTelefon.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_tel");
            txtFax.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_fax");
            txtEmail.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_email");
            txtAdresa.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_adresa");
            txtAdresaWeb.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_web");
            txtOras.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_oras");
            txtJudet.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_jud");
            txtCapSocial.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_capsoc");

            rbDa.Checked = dsFirma.Tables["firma"].Rows[0]["firma_tva"].Equals("1");
            rbNu.Checked = (!rbDa.Checked);

       
        }

        private void btnAnuleaza_Click(object sender, EventArgs e)
        {
            EnableDisableControls(false);

            dsFirma = new DataSet();
            SqlDataAdapter daFirma = new SqlDataAdapter("SELECT TOP 1 * FROM firma ORDER BY firma_data DESC", conn);
            daFirma.Fill(dsFirma, "firma");

            SetControlDataBinding(dsFirma);

            crManager = (CurrencyManager)BindingContext[dsFirma.Tables["firma"]];
           
        }

    }
}
